<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="80px">
      <el-form-item label="0-2岁身高" prop="height0">
        <el-input v-model="dataForm.height0" placeholder="0-2岁身高" :disabled=disable0></el-input>
      </el-form-item>
      <el-form-item label="2-6岁身高" prop="height1">
        <el-input v-model="dataForm.height1" placeholder="2-6岁身高" :disabled=disable1></el-input>
      </el-form-item>
      <el-form-item label="体重-3SD" prop="weight0">
        <el-input v-model="dataForm.weight0" placeholder="体重-3SD"></el-input>
      </el-form-item>
      <el-form-item label="体重-2SD" prop="weight1">
        <el-input v-model="dataForm.weight1" placeholder="体重-2SD"></el-input>
      </el-form-item>
      <el-form-item label="体重-1SD" prop="weight2">
        <el-input v-model="dataForm.weight2" placeholder="体重-1SD"></el-input>
      </el-form-item>
      <el-form-item label="体重均值" prop="weight3">
        <el-input v-model="dataForm.weight3" placeholder="体重均值"></el-input>
      </el-form-item>
      <el-form-item label="体重1SD" prop="weight4">
        <el-input v-model="dataForm.weight4" placeholder="体重1SD"></el-input>
      </el-form-item>
      <el-form-item label="体重2SD" prop="weight5">
        <el-input v-model="dataForm.weight5" placeholder="体重2SD"></el-input>
      </el-form-item>
      <el-form-item label="体重3SD" prop="weight6">
        <el-input v-model="dataForm.weight6" placeholder="体重3SD"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-select v-model="dataForm.gender" placeholder="请选择">
          <el-option label="男" :value="0"></el-option>
          <el-option label="女" :value="1"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {
    getChildrenHeightObj,
    addChildrenHeightObj,
    putChildrenHeightObj
  } from '@/api/pvm/childrenheight'

  export default {
    data() {
      var childrenTestRule = (rule, value, callback) => {
        //console.log(this.dataForm.height0)
        //console.log(this.dataForm.height1)
        if (this.dataForm.height0 != "" || this.dataForm.height1 != "") {
          if (this.dataForm.height0 != "") {
            this.disable1 = true
          } else if (this.dataForm.height1 != "") {
            this.disable0 = true
          }
          //console.log("不为空")
          callback()
        } else {
          callback(new Error("请输入身高"))
        }
      };
      return {
        visible: false,
        disable0: false,
        disable1: false,
        dataForm: {
          id: 0,
          height0: '',
          height1: '',
          weight0: '',
          weight1: '',
          weight2: '',
          weight3: '',
          weight4: '',
          weight5: '',
          weight6: '',
          gender: '',
          createTime: '',
          updateTime: '',
          createUser: '',
          updateUser: '',
          unitId: '',
          tenantId: ''
        },
        dataRule: {
          height0: [{
            validator: childrenTestRule,
            trigger: 'blur'
          }],
          height1: [{
            validator: childrenTestRule,
            trigger: 'blur'
          }],
          weight0: [{
            required: true,
            message: '体重-3SD不能为空',
            trigger: 'blur'
          }],
          weight1: [{
            required: true,
            message: '体重-2SD不能为空',
            trigger: 'blur'
          }],
          weight2: [{
            required: true,
            message: '体重-1SD不能为空',
            trigger: 'blur'
          }],
          weight3: [{
            required: true,
            message: '体重均值不能为空',
            trigger: 'blur'
          }],
          weight4: [{
            required: true,
            message: '体重1SD不能为空',
            trigger: 'blur'
          }],
          weight5: [{
            required: true,
            message: '体重2SD不能为空',
            trigger: 'blur'
          }],
          weight6: [{
            required: true,
            message: '体重3SD不能为空',
            trigger: 'blur'
          }],
          gender: [{
            required: true,
            message: '性别不能为空',
            trigger: 'blur'
          }],
          // createTime: [
          //   { required: true, message: '创建时间不能为空', trigger: 'blur' }
          // ],
          // updateTime: [
          //   { required: true, message: '修改时间不能为空', trigger: 'blur' }
          // ],
          // createUser: [
          //   { required: true, message: '创建用户id不能为空', trigger: 'blur' }
          // ],
          // updateUser: [
          //   { required: true, message: '修改用户id不能为空', trigger: 'blur' }
          // ],
          // unitId: [
          //   { required: true, message: '单位id不能为空', trigger: 'blur' }
          // ],
          // tenantId: [
          //   { required: true, message: '	租户id不能为空', trigger: 'blur' }
          // ]
        }
      }
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.disable1 = false
        this.disable0 = false
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getChildrenHeightObj(this.dataForm.id).then(response => {
              if(response.data.code == '0'){
                this.dataForm = response.data.data
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              putChildrenHeightObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('修改成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
              })
            } else {
              addChildrenHeightObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('添加成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
              })
            }
          }
        })
      }
    }
  }
</script>
